package com.study.jyg.rxjava2study.activity;

import android.os.Bundle;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.android.material.appbar.AppBarLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.appcompat.widget.Toolbar;
import android.util.Log;

import com.study.jyg.rxjava2study.R;
import com.study.jyg.rxjava2study.base.BasicActivity;
import com.study.jyg.rxjava2study.utils.AdapterInit;

import java.util.Arrays;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * rxjava 选择页面
 */
public class RxjavaStudyActivity extends BasicActivity {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.appBar)
    AppBarLayout appBar;

    private BaseQuickAdapter<String, BaseViewHolder> adapter;

    private String[] datas = {"基本创建", "快速创建&发送事件", "延迟创建"};

    @Override
    protected int getLayout() {
        return R.layout.activity_rxjava_study;
    }

    @Override
    protected void initView() {
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(v -> finish());
        initAdapter();
        observable.subscribe(observer);
    }

    private void initAdapter(){
        adapter = AdapterInit.initRecyclerView(recyclerView, R.layout.item_recyclerview_textview, Arrays.asList(datas), new LinearLayoutManager(mContext), (helper, item) -> {
            helper.setText(R.id.tvItem, item);
        }, (adapter1, view, position) -> {
            Bundle bundle = new Bundle();
            bundle.putInt("type", position);
            startActivity(RxjavaBaseCreateActivity.class, bundle);
        });
    }

    /**
     * //1、观察者创建一个Observer
     */
    Observer<String> observer = new Observer<String>() {
        @Override
        public void onSubscribe(Disposable d) {
            Log.d(TAG, "onSubscribe");
        }

        @Override
        public void onNext(String o) {
            Log.d(TAG, "onNext");
        }

        @Override
        public void onError(Throwable e) {
            Log.d(TAG, "onError");
        }

        @Override
        public void onComplete() {
            Log.d(TAG, "onComplete");
        }
    };

    Observable<String> observable = Observable.create(emitter -> {
        emitter.onNext("1");
        emitter.onNext("1");
        emitter.onComplete();
    });

}
